package net.happy2v.louis.re;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 메인 화면의 아이템을 나타내주는 어댑터
 * - 레이아웃 설정
 * - 아이템 설정
 * 
 * @author happy2v
 *
 */
public class RevivalEnglishAdapter extends BaseAdapter {
	
	public static final int MAIN_MENU_STARTING 		= 0x001;
	public static final int MAIN_MENU_STANDING 		= 0x002;
	public static final int MAIN_MENU_WALKING 		= 0x003;
	public static final int MAIN_MENU_RUNNING 		= 0x004;
	public static final int MAIN_MENU_JUMPING 		= 0x005;
	public static final int MAIN_MENU_FLYING 		= 0x006;
	
	/* Activity 의 문맥을 받는다. */
	private Activity mActivity;
	/* 리스트 메뉴를 저장한다. */
	private ArrayList<RevivalEnglishMenuItem> mMenuItems = new ArrayList<RevivalEnglishMenuItem>();
	/* Item Layout */
	private int mLayout;

	/**
	 * RevivalEnglishAdapter 생성자
	 * @param activity Activity 문맥을 넘겨 받는다.
	 */
	public RevivalEnglishAdapter(Activity activity){
		/* mActivity 에 Activity로 받은 문맥을 저장한다. */
		this.mActivity = activity;
		this.mLayout = R.layout.item_main;
		
		getMenuItem();
	}
	
	private void getMenuItem(){
		RevivalEnglishMenuItem item = new RevivalEnglishMenuItem();
		item.tag = MAIN_MENU_STARTING;
		item.menuItemIcon = mActivity.getResources().getDrawable(R.drawable.icon);
		item.menuItemTitle = mActivity.getString(R.string.main_menu1);
		mMenuItems.add(item);
		item = new RevivalEnglishMenuItem();
		item.tag = MAIN_MENU_STANDING;
		item.menuItemIcon = mActivity.getResources().getDrawable(R.drawable.icon);
		item.menuItemTitle = mActivity.getString(R.string.main_menu2);
		mMenuItems.add(item);
		item = new RevivalEnglishMenuItem();
		item.tag = MAIN_MENU_WALKING;
		item.menuItemIcon = mActivity.getResources().getDrawable(R.drawable.icon);
		item.menuItemTitle = mActivity.getString(R.string.main_menu3);
		mMenuItems.add(item);
		item = new RevivalEnglishMenuItem();
		item.tag = MAIN_MENU_RUNNING;
		item.menuItemIcon = mActivity.getResources().getDrawable(R.drawable.icon);
		item.menuItemTitle = mActivity.getString(R.string.main_menu4);
		mMenuItems.add(item);
		item = new RevivalEnglishMenuItem();
		item.tag = MAIN_MENU_JUMPING;
		item.menuItemIcon = mActivity.getResources().getDrawable(R.drawable.icon);
		item.menuItemTitle = mActivity.getString(R.string.main_menu5);
		mMenuItems.add(item);
		item = new RevivalEnglishMenuItem();
		item.tag = MAIN_MENU_FLYING;
		item.menuItemIcon = mActivity.getResources().getDrawable(R.drawable.icon);
		item.menuItemTitle = mActivity.getString(R.string.main_menu6);
		mMenuItems.add(item);
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		if(convertView == null){
			view = newView(parent);
		}else{
			view = convertView;
		}
		
		bindView(view, position);
		
		return view;
	}
	
	private View newView(ViewGroup parent){
		LayoutInflater inflater = LayoutInflater.from(mActivity);
		View view = inflater.inflate(mLayout, parent, false);
		
		MenuItemHolder holder = new MenuItemHolder();
		holder.iViewItemMainIcon = (ImageView) view.findViewById(R.id.iViewItemMainIcon);
		holder.tViewItemMainTitle = (TextView) view.findViewById(R.id.tViewItemMainTitle);
		
		view.setTag(holder);
		return view;
	}
	
	private void bindView(View view, int position){
		RevivalEnglishMenuItem menuItem = mMenuItems.get(position);
		MenuItemHolder holder = (MenuItemHolder)view.getTag();
		if(menuItem.menuItemIcon != null){
			holder.iViewItemMainIcon.setVisibility(View.VISIBLE);
			holder.iViewItemMainIcon.setImageDrawable(menuItem.menuItemIcon);
		}else{
			holder.iViewItemMainIcon.setVisibility(View.GONE);
		}
		holder.tViewItemMainTitle.setText(menuItem.menuItemTitle);
	}
	
	private static class MenuItemHolder{
		ImageView iViewItemMainIcon;
		TextView tViewItemMainTitle;
	}

	public int getCount() {
		return mMenuItems.size();
	}

	public Object getItem(int position) {
		return mMenuItems.get(position);
	}

	public long getItemId(int position) {
		return mMenuItems.get(position).tag;
	}
}
